import Vue from "vue";
import VueRouter from "vue-router";

import Sindex from "@/components/pc/sindex/sindex";
import workingCondition from "@/components/pc/workingCondition/workingCondition";
import DataDirectory from "@/components/pc/DataDirectory/DataDirectory";
import AppListPage from "@/components/pc/AppListPage/AppListPage";
import Interaction from "@/components/pc/Interaction/Interaction";
import OpenStatistics from "@/components/pc/OpenStatistics/OpenStatistics";
import Serch from "@/components/pc/serch/serch";
import Sence from "@/components/pc/Sence/Sence";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/sindex",
  },
  {
    path: "/sindex",
    name: "sindex",
    meta: {
      title: "首页",
      index: 0,
    },
    component: Sindex,
  },
  {
    path: "/workingCondition",
    name: "workingCondition",
    meta: {
      title: "工作状态",
      index: 0,
    },
    component: workingCondition,
  },
  {
    path: "/DataDirectory",
    name: "DataDirectory",
    meta: {
      title: "数据目录",
      index: 0,
    },
    component: DataDirectory,
  },
  {
    path: "/AppListPage",
    name: "AppListPage",
    meta: {
      title: "APP应用",
      index: 0,
    },
    component: AppListPage,
  },
  {
    path: "/Interaction",
    name: "Interaction",
    meta: {
      title: "互动交流",
      index: 0,
    },
    component: Interaction,
  },
  {
    path: "/OpenStatistics",
    name: "OpenStatistics",
    meta: {
      title: "开发统计",
      index: 0,
    },
    component: OpenStatistics,
  },
  {
    path: "/serch",
    name: "serch",
    meta: {
      title: "搜索",
      index: 0,
      needLogin: false,
    },
    component: Serch,
  },
  {
    path: '/sence',
    name: "sence",
    meta: {
      title: "管理",
      index: 0,
      needLogin: false,
    },
    component: Sence,
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  const token = su.cook.getcookie("token");
  if (to.name == "sindex" || !to.meta.needLogin) {
    next();
  } else {
    if (token && token != "") {
      next();
    } else {
      // alert("请登录");
    }
  }
  document.title = "广元数据开放平台";
  //判断是否进入的login页（需有单独的登录页）
  // if (to.name == "login") {
  //   if (token) {
  //     next("/home");
  //   } else {
  //     next();
  //   }
  // } else {
  //   if (token) {
  //     next();
  //   } else {
  //     next("/login");
  //   }
  // }
  // document.title = to.meta.title || "这里写项目名";
});
const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);

  return originalPush.call(this, location).catch(err => err);
};

export default router;
